import requests
import base64
import json

app_key = '204492660'
app_secret = 'fmpMLl09XaChhtuIT2fTf106EB4auTF7'
app_code = 'c5d773e45a9e4165a987efa3fc74a706'


class AiTongue:
    def __init__(self, app_code=None, source_code=0):
        self.app_code = app_code or 'c5d773e45a9e4165a987efa3fc74a706'
        self.source_code = source_code or 0
        self.host = 'https://ali-market-tongue-detect.macrocura.com'
        self.session_id = None

    def upload_image(self, image_file=None):
        '''创建会话'''
        if not image_file:
            print("图片不存在")
            return {}
        headers = {
            'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            'Authorization': 'APPCODE ' + self.app_code
        }
        path = '/diagnose/tongue/match/question/'
        url = self.host + path
        with open(image_file, 'rb') as f:
            data = {
                'source_code': self.source_code,
                'image': base64.b64encode(f.read())
            }
            resp = requests.post(url, data=data, headers=headers)
            result = resp.json()
            print(result)
            res = result.get('data')
            self.session_id = res.get('session_id') 
            return res

    def get_report(self, res):
        '''获取报告'''
        answer_type = 1
        if not self.session_id:
            print('请先拍照上传')
            return 
        headers = {
            'Content-Type': 'application/json; charset=UTF-8',
            'Authorization': 'APPCODE ' + self.app_code
        }
        path = '/diagnose/tongue/inquiry/report/'
        url = self.host + path
        if isinstance(res, str):
            res = json.loads(res)
        # data = {
        #         'session_id': self.session_id,
        #         'basic_answers': [
        #             {
        #                 "id": 5,
        #                 "answer": "2ca801d5-108e-4354-af86-622736ea157e"
        #             },
        #             {
        #                 "id": 22,
        #                 "answer": "09167e22-be6d-4681-9afd-72cd6ebc7c9b"
        #             }
        #         ],
        #         'addition_answers': [
        #             {
        #                 "id": 1000000,
        #                 "answer": "1"
        #             }
        #         ]
        #     }
        # print(data)
        # print('-' * 20)
        # resp = requests.post(url, json=data, headers=headers)
        # result = resp.json()
        # print(result)
        # res = result.get('data', {})
        # answer_type = res.get('answer_type')
        # self.session_id = res.get('session_id')
        # if answer_type == 2:
        #     return res
        # elif answer_type == 3:
        #     print('信息不足')
        #     return
        basic_questions = res.get('basic_questions')
        if basic_questions:
            data= {
                'basic_answers': [
                    {
                        "id": i.get('id'),
                        "answer": i.get('options', [])[0].get('value')
                    } for i in basic_questions
                ]
            }
        else:
            data = {}
        while True:
            addition_questions = res.get('addition_questions')
            data['session_id'] =  self.session_id
            if addition_questions:
                data['addition_answers'] = [
                    {
                        "id": i.get('id'),
                        "answer": i.get('options', [])[0].get('value')
                    } for i in addition_questions
                ]
            print(data)
            print('-' * 20)
            resp = requests.post(url, json=data, headers=headers)
            result = resp.json()
            print(result)
            res = result.get('data', {})
            if not res:
                print('查询失败')
                return
            answer_type = res.get('answer_type')
            self.session_id = res.get('session_id')
            if answer_type == 2:
                return res.get('report', {})
            elif answer_type == 3:
                print('信息不足')
                return
